
//
//  VCOfSearchConent.m
//  XYFMApp
//
//  Created by 岁变 on 16/9/10.
//  Copyright © 2016年 岁变. All rights reserved.
//

#import "VCOfSearchConent.h"
#import "XYMacro.h"

@interface VCOfSearchConent () <UICollectionViewDelegate, UICollectionViewDataSource, TopPageBarDelegate>
@property (nonatomic, strong) UIView *customNavi;
@property (nonatomic, strong) UITextField *searchField;
@property (nonatomic, strong) UIButton *searchButton;
@property (nonatomic, strong) UIButton *backButton;
@property (nonatomic, strong) BaseCollectionView *myCollection;
@property (nonatomic, strong) DiscoverTopPageBar *topBar;
@property (nonatomic, strong) SearchModelFirst *columnOfUpdataModel;
@property (nonatomic, strong) SearchModelFirst *columnOfNumberModel;
@property (nonatomic, strong) SearchModelSecond *programUpdataModel;
@property (nonatomic, strong) SearchModelSecond *programNumberModel;
@property (nonatomic, strong) SearchModelFirst *anchorModel;
@end

@implementation VCOfSearchConent

- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
    [self configNavigationController];
    [self configViewController];
    
}


- (void)handleData {
    
    NSString *str1 = @"json=%7B%22cat_id%22%3A%220%22%2C%22orderby%22%3A0%2C%22pagination%22%3A%7B%22count%22%3A54%2C%22page%22%3A1%7D%2C%22searchtype%22%3A0%2C%22session%22%3A%7B%22uid%22%3A%2233376%22%2C%22sid%22%3A%22f219a5f641104487a9cb7b437c190335%22%7D%2C%22keyword%22%3A%22";
    NSString *str2 = @"%22%2C%22target_id%22%3A%220%22%7D";
        NSString *body1 = [[str1 stringByAppendingString:_keyword] stringByAppendingString:str2];
    
    [XYNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/itemlist.jhtml" andBody:body1 andBodyStyle:XYBodyString andHeader:URLHEADER andResponse:XYJSON andSuccessBlock:^(id result) {
        _columnOfUpdataModel = [SearchModelFirst modelWithDictionary:result];
        _columnOfUpdataModel.tag = 1;
        [_myCollection reloadData];
        if (_columnOfUpdataModel.data.count == 0) {
            [self createPromptBox];
        }
        
    } andFailureBlock:^(NSError *error) {}];
    
    
    NSString *str3 = @"json=%7B%22cat_id%22%3A%220%22%2C%22orderby%22%3A1%2C%22pagination%22%3A%7B%22count%22%3A54%2C%22page%22%3A1%7D%2C%22searchtype%22%3A0%2C%22session%22%3A%7B%22uid%22%3A%2233376%22%2C%22sid%22%3A%22f219a5f641104487a9cb7b437c190335%22%7D%2C%22keyword%22%3A%22";
    NSString *body2 = [[str3 stringByAppendingString:_keyword] stringByAppendingString:str2];
    
    [XYNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/itemlist.jhtml" andBody:body2 andBodyStyle:XYBodyString andHeader:URLHEADER andResponse:XYJSON andSuccessBlock:^(id result) {
        _columnOfNumberModel = [SearchModelFirst modelWithDictionary:result];
        _columnOfNumberModel.tag = 2;
        [_myCollection reloadData];
    } andFailureBlock:^(NSError *error) {}];
    
    NSString *str4 = @"json=%7B%22pagination%22%3A%7B%22count%22%3A25%2C%22page%22%3A1%7D%2C%22orderBy%22%3A0%2C%22session%22%3A%7B%22uid%22%3A%2233376%22%2C%22sid%22%3A%22f219a5f641104487a9cb7b437c190335%22%7D%2C%22recomm%22%3A0%2C%22item_id%22%3A%220%22%2C%22keyword%22%3A%22";
    NSString *str5 = @"%22%7D";
    NSString *body3 = [[str4 stringByAppendingString:_keyword] stringByAppendingString:str5];
    
    [XYNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/musiclist.jhtml" andBody:body3 andBodyStyle:XYBodyString andHeader:URLHEADER andResponse:XYJSON andSuccessBlock:^(id result) {
        _programUpdataModel = [SearchModelSecond modelWithDictionary:result];
        _programUpdataModel.tag = 1;
        [_myCollection reloadData];
    } andFailureBlock:^(NSError *error) {}];
    
    
    NSString *str6 = @"json=%7B%22pagination%22%3A%7B%22count%22%3A25%2C%22page%22%3A1%7D%2C%22orderBy%22%3A9%2C%22session%22%3A%7B%22uid%22%3A%2233376%22%2C%22sid%22%3A%22f219a5f641104487a9cb7b437c190335%22%7D%2C%22recomm%22%3A0%2C%22item_id%22%3A%220%22%2C%22keyword%22%3A%22";
    NSString *body4 = [[str6 stringByAppendingString:_keyword] stringByAppendingString:str5];
    
    [XYNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/musiclist.jhtml" andBody:body4 andBodyStyle:XYBodyString andHeader:URLHEADER andResponse:XYJSON andSuccessBlock:^(id result) {
        _programNumberModel = [SearchModelSecond modelWithDictionary:result];
        _programNumberModel.tag = 2;
        [_myCollection reloadData];
    } andFailureBlock:^(NSError *error) {}];
    

    NSString *str7 = @"json=%7B%22cat_id%22%3A%220%22%2C%22searchtype%22%3A2%2C%22target_name%22%3A%22";
    NSString *str8 = @"%22%2C%22session%22%3A%7B%22uid%22%3A%2233376%22%2C%22sid%22%3A%22f219a5f641104487a9cb7b437c190335%22%7D%2C%22keyword%22%3A%22%22%2C%22orderby%22%3A0%2C%22pagination%22%3A%7B%22count%22%3A54%2C%22page%22%3A1%7D%2C%22target_id%22%3A%220%22%7D";
    NSString *body5 = [[str7 stringByAppendingString:_keyword] stringByAppendingString:str8];
    
    [XYNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/itemlist.jhtml" andBody:body5 andBodyStyle:XYBodyString andHeader:URLHEADER andResponse:XYJSON andSuccessBlock:^(id result) {
        _anchorModel = [SearchModelFirst modelWithDictionary:result];
        [_myCollection reloadData];
    } andFailureBlock:^(NSError *error) {}];

    
    
}

- (void)configNavigationController {
    self.title = @"搜索内容";
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
    UIButton *goBackButton = [UIButton buttonWithType:UIButtonTypeCustom];
    goBackButton.frame = CGRectMake(0, 0, 30, 30);
    [goBackButton setImage:[UIImage imageNamed:@"go_back"] forState:UIControlStateNormal];
    [goBackButton addTarget:self action:@selector(goBack:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:goBackButton];
    self.navigationItem.leftBarButtonItem = leftItem;
    
}


- (void)configViewController {
    self.view.backgroundColor = BACKGROUNDCOLOR;
    [self createTopPageBar];
    [self createCollectionView];
    
}


- (void)createTopPageBar {
    _topBar = [[DiscoverTopPageBar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40) withTitleArr:@[@"栏目", @"节目", @"主播"]];
    _topBar.delegate = self;
    [self.view addSubview:_topBar];
}

- (void)createCollectionView {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - 104);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);

    _myCollection = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH, SCREEN_HEIGHT - 40) collectionViewLayout:layout];
    _myCollection.delegate = self;
    _myCollection.dataSource = self;
    _myCollection.pagingEnabled = YES;
    _myCollection.bounces = NO;
    _myCollection.showsHorizontalScrollIndicator = NO;
    [_myCollection registerClass:[SearchContentCollectionViewCellFirst class] forCellWithReuseIdentifier:@"SearchContentCollectionViewCellFirst"];
    [_myCollection registerClass:[SearchContentCollectionViewCellSecond class] forCellWithReuseIdentifier:@"SearchContentCollectionViewCellSecond"];
    [_myCollection registerClass:[SearchContentCollectionViewCellThird class] forCellWithReuseIdentifier:@"SearchContentCollectionViewCellThird"];
    [self.view addSubview:_myCollection];
}



- (void)goBack:(UIButton *)button {
    
    [self.navigationController popViewControllerAnimated:YES];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.94 green:0.00 blue:0.00 alpha:1.00];
}


- (void)didClickItemindexPath:(NSIndexPath *)indexPath PageBar:(DiscoverTopPageBar *)pageBar {
    
    if (_columnOfUpdataModel.status.succeed == 1 && _columnOfNumberModel.status.succeed == 1 && _anchorModel.status.succeed == 1 && _programNumberModel.status.succeed == 1 && _programNumberModel.status.succeed == 1) {
        
        [_myCollection scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
        
        if (indexPath.row == 2 && _anchorModel.data.count == 0) {
            [self createPromptBox];
        }
        if (indexPath.row == 1 && _programUpdataModel.data.count == 0) {
            [self createPromptBox];
        }
        if (indexPath.row == 0 && _columnOfUpdataModel.data.count == 0) {
            [self createPromptBox];
        }

    }
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if (_columnOfUpdataModel.status.succeed == 1 && _columnOfNumberModel.status.succeed == 1 && _anchorModel.status.succeed == 1 && _programNumberModel.status.succeed == 1 && _programNumberModel.status.succeed == 1) {
        return 3;
    } else {
        return 0;
    }
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.item == 0) {
        SearchContentCollectionViewCellFirst *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SearchContentCollectionViewCellFirst" forIndexPath:indexPath];
        cell.firstModel = _columnOfUpdataModel;
        cell.secondModel = _columnOfNumberModel;
        cell.keyword = _keyword;
        return cell;
    } else if (indexPath.item == 1) {
        SearchContentCollectionViewCellSecond *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SearchContentCollectionViewCellSecond" forIndexPath:indexPath];
        cell.firstModel = _programUpdataModel;
        cell.secondModel = _programNumberModel;
        cell.keyword = _keyword;
        return cell;
    } else {
        SearchContentCollectionViewCellThird *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SearchContentCollectionViewCellThird" forIndexPath:indexPath];
        cell.model = _anchorModel;
        cell.keyword = _keyword;
        return cell;
    }
}


#pragma mark ----------- KVO 键值观察 -----------------
- (void)createKVO {
    //KVO
    [_myCollection addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    
    CGFloat newX = [[change objectForKey:@"new"] CGPointValue].x;
    CGFloat oldX = [[change objectForKey:@"old"] CGPointValue].x;
    
    CGFloat redX = (newX / SCREEN_WIDTH) * (SCREEN_WIDTH / 3);
    // 修改Redline位置.
    self.topBar.redLineView.transform = CGAffineTransformMakeTranslation(redX, 0);
    
    DiscoverCollectionViewCellForTopPageBar *cell1 = (DiscoverCollectionViewCellForTopPageBar *)[_topBar.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:newX / SCREEN_WIDTH inSection:0]];
    DiscoverCollectionViewCellForTopPageBar *cell2 = (DiscoverCollectionViewCellForTopPageBar *)[_topBar.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:oldX / SCREEN_WIDTH inSection:0]];
    cell2.titleLabel.textColor = [UIColor grayColor];
    cell1.titleLabel.textColor = [UIColor whiteColor];
}


- (void)createPromptBox {
    UIWindow *windows = [UIApplication sharedApplication].keyWindow;
    UIView *customPromptBox = [[UIView alloc]init];
    customPromptBox.backgroundColor = BACKGROUNDCOLOR;
    [windows addSubview:customPromptBox];
    [customPromptBox mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).with.offset(0);
        make.right.equalTo(self.view.mas_right).with.offset(0);
        make.top.equalTo(self.view.mas_top).with.offset(144);
        make.bottom.equalTo(self.view.mas_bottom).with.offset(0);
    }];
    
    UIImageView *logoImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"download_nologin"]];
    [customPromptBox addSubview:logoImageView];
    [logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(customPromptBox.mas_centerY);
        make.centerX.equalTo(customPromptBox.mas_centerX);
        make.height.mas_offset(180);
        make.width.mas_offset(180);
    }];
    
    UILabel *textLabel = [[UILabel alloc] init];
    textLabel.text = @"未搜到相关内容";
    textLabel.font = [UIFont systemFontOfSize:24];
    textLabel.textColor = [UIColor whiteColor];
    textLabel.textAlignment = NSTextAlignmentCenter;
    [customPromptBox addSubview:textLabel];
    [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(logoImageView.mas_right).with.offset(-10);
        make.left.equalTo(customPromptBox.mas_left).with.offset(0);
        make.right.equalTo(customPromptBox.mas_right).with.offset(0);
        make.height.mas_offset(80);
    }];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
        //2秒以后移除view
        [customPromptBox removeFromSuperview];
    });
}






- (void)viewWillDisappear:(BOOL)animated {
    [_myCollection removeObserver:self forKeyPath:@"contentOffset"];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [self createKVO];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
